/**
 * @(#) MedicalMoneyProxy.as
 *
 * @date		2011. 03. 25
 * @version		1.0
 * @author		baek kwang hyoun
 * @description	건강보험증 Proxy - Proxy
 * 				건강보험증를 조회를한다.
 */
package com.skt.common_hr.hthic.model
{
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.skt.common_hr.hthic.model.vo.FamilyVO;
	import com.skt.common_hr.hthic.model.vo.HthIcFileUpLoadVO;
	import com.skt.common_hr.hthic.model.vo.hthDetailFamilyVO;
	import com.skt.common_hr.hthic.model.vo.hthIcI_FAM_VO;
	import com.skt.common_hr.hthic.view.components.ReqhthIcViewSac;
	import com.wcl.dataloader.*;
	import com.wcl.utils.DateUtil;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.formatters.PhoneFormatter;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;

	public class HthIcProxy extends CommonProxy
	{
		public static const NAME:String = "HthIcProxy";

		public static const LOAD_MEDICALMONEY_LIST_COMPLETE:String	= "LoadMedicalMoneyListComplete";
		public static const LOAD_MEDICALMONEY_LIST_FAILED:String	= "LoadMedicalMoneyListFailed";


		public var mainList:ArrayCollection;
		public var subTabList:ArrayCollection; 
		public var acOitab:ArrayCollection;
		public var subTabFileList:HthIcFileUpLoadVO;
		
		
		public var key:String;

		//지원기준
		public var loadFaultMessage:String;

		/**
		 * 생성자
		 *
		 * @param
		 * @return void
		 */
		public function HthIcProxy(data:Object=null)
		{
			super(NAME, data);
		}

		// 메인리스트 호출
		public function loadList(pernr:String,  BEGDA:Date, ENDDA:Date, setFunc:Function):void
		{
			var params:Object = new Object();
			params["I_PERNR"] = pernr;
			params["I_BEGDA"] = DateUtil.toBasicString(BEGDA);
			params["I_ENDDA"] = DateUtil.toBasicString(ENDDA);
			
			loadData("Z_HR_0155", params, loadListResult, listFault, setFunc); 
		}
		public function loadListResult(event:ResultEvent):void
		{
			if(retCode==0) {
				mainList = parseMainList(event.result.O_ITAB);
				event.result.func(mainList)
			}
			else {
				Alert.show('loadListResult err: ' + retText);
			}
		}


		// 가족관계
		public var familyList:ArrayCollection;
		public function loadFamily(pernr:String, setFunc:Function):void
		{
			var params:Object = new Object();
			params["I_PERNR"] = pernr;
			loadData("Z_HR_0040", params, loadFamilyResult, listFault, setFunc); 
		}
		public function loadFamilyResult(event:ResultEvent):void
		{
			if(retCode==0) {
				familyList = parseFamilyList(event.result.FTAB);
				event.result.func(familyList) 
			}
			else {
				Alert.show('loadFamilyResult err: ' + retText);
			}
		}

		
		// 가족 상세정보 가져오기.
		public var oHthDetailFamilyVO:hthDetailFamilyVO;		
		public function getFamilyDetail(obj:Object, setFunc:Function):void
		{
			var params:Object = new Object();
			params["I_PERNR"] = Variables.myPernr;
			params["I_AUSPR"] = obj.AUSPR;
			
			loadData("Z_HR_0145", params, getFamilyDetailResult, listFault, setFunc, obj.SEQNR); 
		}
		public function getFamilyDetailResult(event:ResultEvent):void
		{			
			if(retCode==0) {
				var ac:ArrayCollection;//hthDetailFamilyVO;
				//ac = parseDetailFamilyAc(ArrayCollection(event.result.O_ITAB).getItemAt(0) );
				ac = parseDetailFamilyAc(event.result.O_ITAB);				
				event.result.func(ac, event.result.rtnObj);
			}
			else {
				Alert.show(retText);
			}
		}	

		// 리스트 업데이트 
 		public function detailList(selecteItem:Object, setFunc:Function):void
		{
			var params:Object = new Object();
			params["I_BUKRS"] = selecteItem.BUKRS;
			params["I_PERNR"] = selecteItem.PERNR;
			params["I_REQDT"] = selecteItem.REQDT_JSON;
			params["I_REQRN"] = selecteItem.REQRN;
			
			loadData("Z_HR_0142", params, detailListResult, listFault, setFunc); 
		}
		public function detailListResult(event:ResultEvent):void
		{
			if(retCode==0) {
				subTabList 	= parseMainList(event.result.O_ITAB);
				
				subTabFileList 	= new HthIcFileUpLoadVO();
				subTabFileList.I_FILE_NAME = event.result.O_ITAB[0].FILE_NAME;
				subTabFileList.I_FILE_EXT = event.result.O_ITAB[0].FILE_EXT;
				subTabFileList.I_FILE_SIZE = event.result.O_ITAB[0].FILE_SIZE;
				subTabFileList.I_FILE_URL = event.result.O_ITAB[0].FILE_URL;
				
				acOitab		= parseSubList(event.result.O_FAM);
				
				var obj:Object = {};				
				obj.acOitab		= subTabList;
				obj.acOfam	 	= acOitab;
				obj.fileInfos	= subTabFileList;
				event.result.func(obj)
			}
			else {
				Alert.show('detailListResult err: ' + retText);
			}
		}	 
		
		
		// 삭제
 		public function deleteList(selecteItem:Object, setFunc:Function):void
		{
			var params:Object = new Object();
			params["I_BUKRS"] = selecteItem.BUKRS;
			params["I_PERNR"] = selecteItem.PERNR;
			params["I_REQDT"] = selecteItem.REQDT_JSON;
			params["I_REQRN"] = selecteItem.REQRN;
			
			loadData("Z_HR_0146", params, deleteListResult, listFault, setFunc); 
		}
		public function deleteListResult(event:ResultEvent):void
		{
			if(retCode==0) {
				event.result.func();
			}
			else {
				Alert.show('detailListResult err: ' + retText);
			}
		}			
		
		// 재발급 사유 코드
		public var acReReqRsnCode:ArrayCollection;		
		public function getReReqRsnCode(setFunc:Function):void
		{
			var params:Object = new Object();
			params["I_DNAME"] = "ZDWHYRE";
			loadData("Z_HR_0003", params, getReReqRsnCodeResult, listFault, setFunc); 
		}
		public function getReReqRsnCodeResult(event:ResultEvent):void
		{			
			if(retCode==0) {
				var obj:Object = {CODE:"0", TEXT:"재발급사유를 선택하세요"};
				acReReqRsnCode = event.result.ITAB;
				acReReqRsnCode.addItemAt(obj, 0);
				event.result.func(acReReqRsnCode);
			}
			else {
				Alert.show('getReReqRsnCodeResult err: ' + retText);
			}
		}

		// 관계 코드조회
		public var acFamilyCode:ArrayCollection;		
		public function getFamilyCode(setFunc:Function):void
		{
			var params:Object = new Object();
			loadData("Z_HR_0144", params, getFamilyCodeResult, listFault, setFunc); 
		}
		public function getFamilyCodeResult(event:ResultEvent):void
		{			
			if(retCode==0) {
				var obj:Object = {CODE:"0", ATEXT:"선택하세요", AUSPR:"0"};
				acFamilyCode = event.result.O_ITAB;
				acFamilyCode.addItemAt(obj, 0);
				event.result.func(acFamilyCode);
			}
			else {
				Alert.show('getFamilyCodeResult err: ' + retText);
			}
		}
		
		// 변경신청항목 코드조회
		public var acEditListCode:ArrayCollection;		
		public function getEditListCode(setFunc:Function):void
		{
			var params:Object = new Object();
			params["I_DNAME"] = "ZDCWHAT";
			loadData("Z_HR_0003", params, getEditListCodeResult, listFault, setFunc); 
		}
		public function getEditListCodeResult(event:ResultEvent):void
		{			
			if(retCode==0) {
				var obj:Object = {CODE:"0", TEXT:"선택하세요"};
				acEditListCode = event.result.ITAB;
				acEditListCode.addItemAt(obj, 0);
				event.result.func(acEditListCode);
			}
			else {
				Alert.show('getEditListCodeResult err: ' + retText);
			}
		}
		
		// 피부양자 신고 구분
		public var acDependantsDvCode:ArrayCollection;		
		public function getDependantsDvCode(setFunc:Function):void
		{
			var params:Object = new Object();
			params["I_DNAME"] = "ZDIETYP";
			loadData("Z_HR_0003", params, getDependantsDvCodeResult, listFault, setFunc); 
		}
		public function getDependantsDvCodeResult(event:ResultEvent):void
		{			
			if(retCode==0) {
				var obj:Object = {CODE:"0", TEXT:"선택하세요"};
				acDependantsDvCode = event.result.ITAB;
				acDependantsDvCode.addItemAt(obj, 0);
				event.result.func(acDependantsDvCode);
			}
			else {
				Alert.show('getDependantsDvCodeResult err: ' + retText);
			}
		}
		
		// 추가/제외사유
		public var acDependantsRsnCode:ArrayCollection;		
		public function getDependantsRsnCode(setFunc:Function):void
		{
			var params:Object = new Object();
			params["I_DNAME"] = "ZDIERSN";
			loadData("Z_HR_0003", params, getDependantsRsnCodeResult, listFault, setFunc); 
		}
		public function getDependantsRsnCodeResult(event:ResultEvent):void
		{			
			if(retCode==0) {
				var obj:Object = {CODE:"0", TEXT:"선택하세요"};
				acDependantsRsnCode = event.result.ITAB;
				acDependantsRsnCode.addItemAt(obj, 0);
				event.result.func(acDependantsRsnCode);
			}
			else {
				Alert.show('getDependantsRsnCodeResult err : ' + retText);
			}
		}
	
		
										
		// 저장
		public function saveHtIc(iTab:uint, oData:Object, ac:ArrayCollection, fileVo:HthIcFileUpLoadVO, setFunc:Function):void
		{
			var tables:Object = new Object();
			var table:Array = new Array();			
			var params:Object = new Object();
			
			if(oData.sManager=="U") {	// update일경우만 수정일 필요함.
				params["I_BUKRS"] = oData.BUKRS;	//회사코드			
				params["I_REQDT"] = oData.REQDT;	//신청일
			}
			
			params["I_TODO"] 	= oData.sManager;	//I:신규, U:수정
			params["I_PERNR"] 	= Variables.myPernr;	//사원번호
			params["I_REQRN"] 	= (iTab+1).toString();	//신청사유			
			if(iTab == 0) {

			}
			//table.push(toJsonType(vo));
			var obj:Object = {};
			obj.WHYRE = oData.WHYRE;
			
			// file Vo 관련
			if(fileVo.I_FILE_NAME!=null) 
			{
				obj.FILE_NAME = fileVo.I_FILE_NAME;
				obj.FILE_EXT = fileVo.I_FILE_EXT;
				obj.FILE_URL = fileVo.I_FILE_URL;
				obj.FILE_SIZE = fileVo.I_FILE_SIZE.toString();
			} 
			
			table.push(obj);

			var pidNum:PhoneFormatter = new PhoneFormatter;
			pidNum.formatString = "#############";
						
 			var tSub_tabArr:Array = new Array();
	 		for each(var rec:hthIcI_FAM_VO in ac)
	 		{
				
	 			var data:Object = new Object;
	 			data.AUSPR = rec.AUSPR;
	 			data.CTO 	= rec.CTO;
	 			data.CWHAT 	= rec.CWHAT;
	 			data.ENAME 	= rec.ENAME;
	 			data.HOUSE 	= rec.HOUSE;
	 			data.IEDAT 	= DateUtil.toBasicString(rec.IEDAT);
	 			data.IERSN 	= rec.IERSN;
	 			data.IETYP 	= rec.IETYP;
	 			//data.REGNO 	= pidNum.format(rec.REGNO);
	 			
	 			data.REGNO 	= String(rec.REGNO).replace('-', '');
				if(data.REGNO == "\"null\"" || data.REGNO == "null"){
					data.REGNO = "";
				}
				if(data.REGNO != null && data.REGNO != "" ) {
					tSub_tabArr.push(data);	
				}
	 			
	 		}
			

			
			tables["I_ITAB"] = table;
			tables["I_FAM"] = tSub_tabArr;
			saveData("Z_HR_0143", params, tables, saveHtIcResult, listFault, setFunc); 
		}
		public function saveHtIcResult(event:ResultEvent):void
		{			
			if(retCode==0) {
				event.result.func();
			}			
			else {
				Alert.show(retText);
			}
		}



		private function parseMainList(recArr:ArrayCollection):ArrayCollection
		{
			var retArr:ArrayCollection = new ArrayCollection();
			
			for each(var obj:Object in recArr){
				//var retObj:MedicalSubTabVO = new MedicalSubTabVO();			
				var retObj:Object = new Object();
				for(var field:String in obj) {					
					if(field == "REQDT" || field == "SENDT" ) {					
						retObj[field+'_DT'] = DateUtil.stringToDateTime(obj[field]);
						retObj[field+'_TX'] = DateUtil.toDefaultString(retObj[field+'_DT']);
						retObj[field+'_JSON'] = DateUtil.toBasicString(retObj[field+'_DT']);
						retObj[field] = obj[field];
					}
					else if(field == "BETRG") {
						retObj[field] = Math.round(obj[field]*100);
					}
					else {
						retObj[field] = obj[field];
					}					
				} 
				retArr.addItem(retObj);				
			}
			return retArr;
		}
		
		public var pidNum:PhoneFormatter = new PhoneFormatter;
		private function parseSubList(recArr:ArrayCollection):ArrayCollection
		{
			var retArr:ArrayCollection = new ArrayCollection();
			var idx:int = 0;
			pidNum.formatString = "######-#######";
			for each(var obj:Object in recArr){
				//var retObj:MedicalSubTabVO = new MedicalSubTabVO();			
				var retObj:hthIcI_FAM_VO = new hthIcI_FAM_VO();
				retObj['idx'] = idx;
				idx++; 
				for(var field:String in obj) {					
					if(field == "REQDT" || field == "SENDT" || field == "IEDAT") {					
						retObj[field+'_DT'] = DateUtil.stringToDateTime(obj[field]);
						retObj[field+'_TX'] = DateUtil.toDefaultString(retObj[field+'_DT']);
						retObj[field+'_JSON'] = DateUtil.toBasicString(retObj[field+'_DT']);
						retObj[field] = DateUtil.stringToDateTime(obj[field]);
					}
					else if(field == "BETRG") {
						retObj[field] = Math.round(obj[field]*100);
					}
					else if(field == "REGNO") {
						if(obj[field].length == 13) {
							retObj[field] = pidNum.format(obj[field]);	
						} else {
							retObj[field] = obj[field];
						}						
					}
					else {
						retObj[field] = obj[field];
					}					
				}
				retArr.addItem(retObj);				
			}
			return retArr;
		}
				
		private function toJsonType(vo:Object):Object {
			var retObj:Object = new Object;
			for(var field:String in vo) {
				if(vo[field] is Date) {
					retObj[field] = DateUtil.toBasicString(vo[field]);	
				}
				else {
					retObj[field] = vo[field];	
				}
				trace(retObj[field]);
			} 
			return retObj;
		}
		
		private function listFault(event:FaultEvent):void
		{
			Alert.show('HthIcProxy.as listFault err: ' + retText);
		}		
		
		// VO 상세 가족조회
		private function parseDetailFamilyAc(recArr:ArrayCollection):ArrayCollection//hthDetailFamilyVO
		{
			var retArr:ArrayCollection = new ArrayCollection();
			

			for each(var obj:Object in recArr){
				var retObj:hthDetailFamilyVO = new hthDetailFamilyVO();
				for(var field:String in obj) {					
					if(field == "REQDT" || field == "SENDT" || field == "IEDAT") {					
						retObj[field+'_DT'] = DateUtil.stringToDateTime(obj[field]);
						retObj[field+'_TX'] = DateUtil.toDefaultString(retObj[field+'_DT']);
						retObj[field+'_JSON'] = DateUtil.toBasicString(retObj[field+'_DT']);
						retObj[field] = DateUtil.stringToDateTime(obj[field]);
					}
					else if(field == "BETRG") {
						retObj[field] = Math.round(obj[field]*100);
					}
					else {
						retObj[field] = obj[field];
					}					
				}
				retArr.addItem(retObj);				
			}
			return retArr;
		}
		
		private function parseFamilyList(srcArr:ArrayCollection):ArrayCollection
		{
			var retArr:ArrayCollection = new ArrayCollection();
			
			for each(var rec:Object in srcArr) 
			{
				var vo:FamilyVO = new FamilyVO();
				vo.KDSVH		= rec["KDSVH"];		//자녀와의 관계
				vo.FAMILY		= rec["FAMILY"];	//가족코드
				vo.ZTEXT		= rec["ZTEXT"];		//경조 가족관계 텍스트
				vo.FNAME		= rec["FNAME"];		//사원 또는 지원자의 포맷된 이름
				//vo.REGNO		= rec["REGNO"];		//주민등록번호 (한국)
				vo.REGNO		= vo.getPid(rec["REGNO"]);
				retArr.addItem(vo);
			}
			return retArr;	
		}
		
		/**
		 * 서버에서 전달된 result 객체를 분석하여 Value Object를 생성
		 * 
		 * @param
		 * @return 관족관계(본인) Value Object
		 */
		private function parseFamilyDefault(srcArr:ArrayCollection):FamilyVO
		{
			var retArr:ArrayCollection = new ArrayCollection();
			
			for each(var rec:Object in srcArr) 
			{
				//if(rec["FAMILY"] == "01"){
					var vo:FamilyVO = new FamilyVO();
					vo.KDSVH		= rec["KDSVH"];				//자녀와의 관계
					vo.FAMILY		= rec["FAMILY"];			//가족코드
					vo.ZTEXT		= rec["ZTEXT"];				//경조 가족관계 텍스트
					vo.FNAME		= rec["FNAME"];				//사원 또는 지원자의 포맷된 이름
					vo.REGNO		= vo.getPid(rec["REGNO"]);  //주민등록번호 (한국)
					break;
				//}
			}
			return vo;	
		}		
	}
}